from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.generics import RetrieveAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from django.utils.functional import SimpleLazyObject
from drfdemo.authentication import CustomAuthentication
from drfdemo.permissions import IsXiaoMingPermission
from opt.pagination import StudentPageNumberPagination
from school.models import Student
from school.serializers import StudentModelSerializer


class ExampleView(APIView):
    # 类属性
    authentication_classes = [CustomAuthentication]

    def get(self, request):
        print(request.user)  # 未登录用户
        # print(type(request.user))
        if request.user.id:
            print("通过验证")
        else:
            print("未通过验证")
        return Response({"msg": "ok"})


class HomeApiView(APIView):
    def get(self, request):
        print(request.user)  # 未登录用户
        # print(type(request.user))
        if request.user.id:
            print("通过验证")
        else:
            print("未通过验证")
        return Response({"msg": "ok"})


from rest_framework.permissions import IsAuthenticated, IsAdminUser, IsAuthenticatedOrReadOnly, AllowAny


#
class HomeInfoApiView(APIView):
    """只能登录用户才能访问"""

    # permission_classes = [IsAuthenticated]  # 表示当前视图中的所有视图，只有被已经登录的站点会员访问
    # permission_classes = [IsAdminUser]  # 表示只有管理员才能访问
    # permission_classes = [IsAuthenticatedOrReadOnly]  # 表示只有管理员可访问且普通用户可查看
    # permission_classes = [AllowAny]  # 开放所有权限
    permission_classes = [IsXiaoMingPermission]  # 自定义权限

    def get(self, request):
        """"""
        return Response({"msg": "ok"})

    def post(self, request):
        """"""
        return Response({"msg": "ok"})


from rest_framework.throttling import UserRateThrottle


class StudentInfoApiView(RetrieveAPIView):
    queryset = Student.objects
    serializer_class = StudentModelSerializer
    # permission_classes = [IsXiaoMingPermission]
    # 限流局部配置
    throttle_classes = [UserRateThrottle]


class DemoMemberApiView(APIView):
    """自定义限流"""
    throttle_scope = "member"

    permission_classes = [IsAdminUser]  # 表示只有管理员才能访问

    def get(self, request):
        return Response({"msg": "ok"})


class DemoVipApiView(APIView):
    """自定义限流"""
    throttle_scope = "vip"

    permission_classes = [IsAdminUser]  # 表示只有管理员才能访问

    def get(self, request):
        return Response({"msg": "ok"})


class DemoVVipApiView(APIView):
    """自定义限流"""
    throttle_scope = "vvip"

    permission_classes = [IsAdminUser]  # 表示只有管理员才能访问

    def get(self, request):
        return Response({"msg": "ok"})


from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter


class Demo4ApiView(ListCreateAPIView, RetrieveUpdateDestroyAPIView):
    queryset = Student.objects
    serializer_class = StudentModelSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]  # 设置view局部过滤条件
    filter_fields = ["sex", "age"]  # 设置过滤字段，http://127.0.0.1:8001/opt/demo4/?age=19 请求age=19的数据，也可多字段校验 age=19&sex=0

    # filter_backends = [OrderingFilter]  # 局部配置排序
    ordering_fields = ["id", "age"]  # 设置排序字段 http://127.0.0.1:8001/opt/demo4/?ordering=age&age=19


from rest_framework.generics import ListAPIView
from stuapi.models import Student
from students.serializers import StudentModelSerializers


class Demo5ApiView(ListAPIView):
    queryset = Student.objects.all()
    serializer_class = StudentModelSerializers
    # pagination_class = None  # 关闭全局配置，使用局部配置关闭全局配置
    # 局部分页功能
    pagination_class = StudentPageNumberPagination


class Demo6ApiView(APIView):
    def get(self, request):
        # try:
        1 / 0
        # except:
        #     return Response({"detail": "0不能作为除数"})
        return Response({"msg": "ok"})
